package com.fisher.checker;

import android.content.Context;
import android.widget.Toast;

import com.honeywell.aidc.AidcManager;
import com.honeywell.aidc.BarcodeReaderInfo;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;

public class ActivateApp {
    public static void encryptUuid(Context context, String uuid) throws Exception {
        String publicKeyStr = SharedPublicKey.loadPublicKey(context);
        PublicKey publicKey = KeyHelper.getPublicKey(publicKeyStr);
        AppPreferences.save2Preferences(context, AppPreferences.KEY_ACTIVATE_ENCRYPTED_UUID, EncryptionHelper.encryptData(uuid, publicKey));
    }

    public static boolean isActivate(Context context) {
        String activateFile = AppPreferences.getFromPreferences(context, AppPreferences.KEY_ACTIVATE_LICENSE_FILE);
        if(null == activateFile){
            return false;
        }
        String encryptedUuid = AppPreferences.getFromPreferences(context, AppPreferences.KEY_ENCRYPTED_UUID);
        try {
            KeyPair keyPair = KeyStoreHelper.getKeyPair();
            String uuid = EncryptionHelper.decryptData(activateFile, keyPair.getPrivate());
            //TODO可以不要
            String deviceId = EncryptionHelper.decryptData(encryptedUuid, keyPair.getPrivate());
            return deviceId.equals(uuid);
        } catch (Exception e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
            return false;
        }


    }

    public static boolean activate(Context context, String privateKeyStr) {
        try {
            PrivateKey privateKey = KeyHelper.getPrivateKey(privateKeyStr);
            String encryptedUid = AppPreferences.getFromPreferences(context, AppPreferences.KEY_ACTIVATE_ENCRYPTED_UUID);
            String uuid = EncryptionHelper.decryptData(encryptedUid, privateKey);
            KeyPair keyPair = KeyStoreHelper.getKeyPair();
            String deviceEncryptedUuid = AppPreferences.getFromPreferences(context, AppPreferences.KEY_ENCRYPTED_UUID);
            String deviceId = EncryptionHelper.decryptData(deviceEncryptedUuid, keyPair.getPrivate());
            if(deviceId.equals(uuid)){
                AppPreferences.save2Preferences(context, AppPreferences.KEY_ACTIVATE_LICENSE_FILE,deviceEncryptedUuid);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, "激活失败 --"+e.getMessage(), Toast.LENGTH_LONG).show();
            return false;
        }
    }



    public static boolean isActivateByScanner(Context context, AidcManager manager) {
        String activateFile = AppPreferences.getFromPreferences(context, AppPreferences.KEY_ACTIVATE_LICENSE_FILE_SCANNER);
        if(null == activateFile){
            return false;
        }
        try {
            String sharedPublicKey = AppPreferences.getSharedPublicKey(context);
            String scannerSeriNo = EncryptionHelper.decryptDataByPublicKey(activateFile, KeyHelper.getPublicKey(sharedPublicKey));
            BarcodeReaderInfo barcodeReaderInfo = manager.listConnectedBarcodeDevices().get(0);
            return barcodeReaderInfo.getScannerSerialNumber().equals(scannerSeriNo);
//            return "12345678".equals(scannerSeriNo);
        } catch (Exception e) {
            Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
            return false;
        }


    }

    public static boolean activateByScanner(Context context, String privateKeyStr, AidcManager manager) {
        try {
            PrivateKey privateKey = KeyHelper.getPrivateKey(privateKeyStr);
            BarcodeReaderInfo barcodeReaderInfo = manager.listConnectedBarcodeDevices().get(0);
            String scannerSerialNumber = barcodeReaderInfo.getScannerSerialNumber();

//            String scannerSerialNumber = "12345678";
            String encryptedSerialNumber = EncryptionHelper.encryptDataByPrivateKey(scannerSerialNumber, privateKey);
            String publicKeyStr = SharedPublicKey.savePublicKeyToPreferences(context);
            String decodeScannerSerialNumber = EncryptionHelper.decryptDataByPublicKey(encryptedSerialNumber, KeyHelper.getPublicKey(publicKeyStr));
            if(scannerSerialNumber.equals(decodeScannerSerialNumber)){
                AppPreferences.save2Preferences(context, AppPreferences.KEY_ACTIVATE_LICENSE_FILE_SCANNER,encryptedSerialNumber);
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(context, "激活失败 --"+e.getMessage(), Toast.LENGTH_LONG).show();
            return false;
        }
    }
}
